package com.scalar.chiptrack.wipProcessView.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.wipProcessView.ProcessViewGraph;
import com.scalar.chiptrack.wipProcessView.dao.WipProcessViewDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *  This action class is used by the wip process view applet.<br>
 *	this action class just reads the productId from the applet and creates the processViewGraph object and writes it to the applet.<br>
 *
 * User: Sujan
 * Date: May 6, 2005
 * Time: 3:57:22 PM
 */
public class WipProcessFlowViewAction extends ChipTrackAction
{
	/**
	 * Process the request from the wipProcessView applet.
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
	{
		ObjectOutputStream outputToApplet = null;
		ObjectInputStream inputFromApplet = null;
        try
		{

			inputFromApplet = new ObjectInputStream ( request.getInputStream () );
			String productId = ( String )inputFromApplet.readObject( );
			inputFromApplet.close ();
			ProcessViewGraph processViewGraph = WipProcessViewDAO.getProcessViewGraph ( null,productId );
			WipProcessViewDAO.setWipProcessViewNodeInfo ( null, processViewGraph.getNodeList (), productId );
			processViewGraph.drawGraph ();
			outputToApplet 	= new ObjectOutputStream( response.getOutputStream() );
			outputToApplet.writeObject ( processViewGraph );
			outputToApplet.flush();
			outputToApplet.close();
		}
		catch ( Exception e )
		{
			System.out.println ( "-----------EXCEPTION IN WIP PROCESS ACTION---------------" );
			e.printStackTrace();
		}
		finally
		{
			if ( outputToApplet != null )
			{
				try
				{
					outputToApplet.close();
				}
				catch ( Exception ignore ) { }
			}
			if ( inputFromApplet != null )
			{
				try
				{
					inputFromApplet.close();
				}
				catch ( Exception ignore ) { }
			}
		}
		return null;
	}
}
